package org.WhyMCA.Utils;

/**
@author Danilo '' Riso >> danilo DOT riso AT gmail DOT com
* @author Androidiani Dev Team
* @version 1.0
*/

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI;
import java.net.URLDecoder;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import org.WhyMCA.Models.WhyMCANewsModel;

import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.Log;

/**
 * Managing XML parsing
 * @author danilo
 *
 */

public class XMLParser{

	private static int hour = 40;
	
	/**
	 * Managing XML parsing
	 * @param XML 
	 * @return 
	 */
		
	/**
	 * Gestisce l'XML che contiene le informazioni sulla ricerca sulla localita'
	 * @param XML 
	 * @return 
	 */
	public static ArrayList<WhyMCANewsModel> getNews (String args) throws XmlPullParserException, IOException {
		// E' meglio fare le variabili locali finche si puo
		String _filterstr="";
		// String  _textstr; Serve? 
		// ---
		//Log.d("Problema: ",args);
		
		// Classe da ritornare al chiamante
		org.WhyMCA.Models.WhyMCANewsModel getNews = new org.WhyMCA.Models.WhyMCANewsModel ();
		ArrayList<WhyMCANewsModel> _gil = new ArrayList ();
		
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		factory.setNamespaceAware(true);
		XmlPullParser xpp = factory.newPullParser();

		xpp.setInput( new StringReader (args));
		int eventType = xpp.getEventType();
		
		while (eventType != XmlPullParser.END_DOCUMENT) {
	
			if(eventType == XmlPullParser.START_DOCUMENT) {
			
				//System.out.println("Start document");
			
			} else if(eventType == XmlPullParser.END_DOCUMENT) {
         
				//System.out.println("End document");
     
			} else if(eventType == XmlPullParser.START_TAG) {
				
				//System.out.println("Start tag "+xpp.getName());
				_filterstr = xpp.getName();
								
			} else if(eventType == XmlPullParser.END_TAG) {
         
				_filterstr="";
     
			} else if(eventType == XmlPullParser.TEXT) {
         		
				if(_filterstr.equals("item")) {
					
					getNews = new org.WhyMCA.Models.WhyMCANewsModel ();
					
				}
				
				if(_filterstr.equals("title")) {
					
					getNews.setTitle(xpp.getText());
					//System.out.println(xpp.getText());
										
				} else
				
				if(_filterstr.equals("link")) {
					
					getNews.setLink(xpp.getText());
					//System.out.println(xpp.getText());
										
				} else
				
				if(_filterstr.equals("description")) {
					
					getNews.setDescription(xpp.getText());
					//System.out.println(xpp.getText());
										
				} else
				
				if(_filterstr.equals("pubDate")) {
					
					getNews.setPubDate(xpp.getText());
					//System.out.println(xpp.getText());
														
					_gil.add(getNews);
					
				}
         		         					
			}
									
			eventType = xpp.next();
     
		}
	
		// Ho finito il parsing.. Adesso posso ritornare l'oggetto _gil di tipo Model.WhyMCANewsModel al chiamante
		return _gil;
		
	}
		
}